package com.hj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hj.mapper.BookInfoMapper;
import com.hj.pojo.Bookinfo;
import com.hj.pojo.HttpResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
public class BookInfoController {
       // 依赖注入
        @Resource
        BookInfoMapper bookInfoMapper;

        @GetMapping("/all")
        public HttpResult all(){
                HttpResult httpResult = new HttpResult();
                httpResult.setCode(200);
                httpResult.setMessage("查询成功");
                httpResult.setData(bookInfoMapper.selectList(null));
                return httpResult ;
        }

        @PostMapping("/add")
        public String add(@RequestBody Bookinfo bk){
                bookInfoMapper.insert(bk);
                return "添加图书OK";
        }

        @PutMapping("/update")
        public HttpResult update(@RequestBody Bookinfo bookinfo){
                HttpResult httpResult = new HttpResult();
                UpdateWrapper updateWrapper = new UpdateWrapper();
                updateWrapper.eq("bid", bookinfo.getBid());
                bookInfoMapper.update(bookinfo, updateWrapper);
                httpResult.setCode(200);
                httpResult.setMessage("修改图书成功");
                return httpResult;
        }

        @PutMapping("/updateId")
        public HttpResult updateId(@RequestBody Bookinfo bookinfo){
                HttpResult httpResult = new HttpResult();
                bookInfoMapper.updateById(bookinfo);
                httpResult.setCode(200);
                httpResult.setMessage("修改图书成功");
                return httpResult;
        }


        @DeleteMapping("/delete/{bid}")
        public String delete(@PathVariable Integer bid){
                QueryWrapper<Bookinfo> wrapper = new QueryWrapper<>();
                wrapper. eq("bid",bid); //删除条件，书号为指定的值
                bookInfoMapper.delete(wrapper);

                return "删除图书OK";
        }

        @DeleteMapping("/del")
        public HttpResult del(){
                HttpResult httpResult = new HttpResult();
                Bookinfo bk = new Bookinfo();
                bk.setTitle("品三国");
                bk.setPrice(200);
                QueryWrapper<Bookinfo> wrapper = new QueryWrapper<>(bk);
                this.bookInfoMapper.delete(wrapper);
                httpResult.setMessage("删除成功");
                return httpResult;
        }
        @DeleteMapping("/delByIds")
        public HttpResult delByIds(){

                HttpResult httpResult = new HttpResult();

                List<Integer> lst = Arrays.asList(1,2);
                this.bookInfoMapper.deleteBatchIds(lst);

                httpResult.setMessage("批量删除成功");
                return httpResult;
        }

        @GetMapping("/selectByIds")
        public HttpResult selectByIds(){

                HttpResult httpResult = new HttpResult();

                List<Integer> lst = Arrays.asList(3,4,5);
                List<Bookinfo> books = this.bookInfoMapper.selectBatchIds(lst);
                httpResult.setData(books);
                httpResult.setMessage("查询成功");

                return httpResult;
        }

        @GetMapping("/page/{size}/{cur}")
        public HttpResult page(@PathVariable Integer size, @PathVariable Integer cur){
                HttpResult httpResult = new HttpResult();
                List<Bookinfo> books = null;
                //MybatisPlus分页，设置每页尺寸和当前页码
                Page<Bookinfo> page = new Page<>(cur,size );
                QueryWrapper wrapper = new QueryWrapper();
                wrapper.eq("type", "小说");

                //按"小说"类型进行分页查询
                IPage<Bookinfo> iPage = bookInfoMapper.selectPage(page, wrapper);

                books = iPage.getRecords(); //总记录数
                String msg = "共有"+iPage.getTotal()+"本图书，共有"+iPage.getPages()+"页，当前是第"+iPage.getCurrent()+"页";

                httpResult.setMessage(msg);
                httpResult.setData(books);

                return httpResult;
        }

        @GetMapping("/t1")
        public List<Bookinfo> t1(){
                QueryWrapper<Bookinfo> wrapper = new QueryWrapper<>();
                //wrapper.between("price", 100, 120);
                wrapper.orderByDesc("price"); //价格由高到低排序

                List<Bookinfo> books = bookInfoMapper.selectList(wrapper);

                return books;
        }

        @GetMapping("/t2")
        public List<Bookinfo> t2(){
                QueryWrapper<Bookinfo> wrapper = new QueryWrapper<>();


                wrapper.likeRight("title", "Java");

                List<Bookinfo> books = bookInfoMapper.selectList(wrapper);

                return books;
        }


}
